﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace PSE.Framework.Configuration
{
    /// <summary>
    /// Constantes com as origens dos logs de erros
    /// </summary>
    public class SystemComponents
    {
        /// <summary>
        /// Sistema
        /// </summary>
        public static readonly SystemComponent System = GetSystem("PSE");

        /// <summary>
        /// UI
        /// </summary>
        public static readonly SystemComponent UserInterface = new SystemComponent("UserInterface", "User Interface", SystemComponentType.Component);

        /// <summary>
        /// Serviços WCF
        /// </summary>
        public static readonly SystemComponent Service = new SystemComponent("Service", "Service", SystemComponentType.Component);

        /// <summary>
        /// Processos do Process Manager
        /// </summary>
        public static readonly SystemComponent Process = new SystemComponent("Process", "Process", SystemComponentType.Component);

        /// <summary>
        /// STS
        /// </summary>
        public static readonly SystemComponent SecurityTokenService = new SystemComponent("SecurityTokenService", "Security Token Service", SystemComponentType.Component);

        /// <summary>
        /// Versions
        /// </summary>
        public static readonly SystemComponent Versions = new SystemComponent("Versions", "Versions", SystemComponentType.Component);

        /// <summary>
        /// Captura o sistema pelo nome
        /// </summary>
        /// <param name="systemName">Nome do sistema</param>
        /// <returns>Sistema</returns>
        public static SystemComponent GetSystem(string systemName)
        {
            return GetSystem(systemName, true);
        }

        /// <summary>
        /// Tenta capturar o sistema pelo nome
        /// </summary>
        /// <param name="systemName">Nome do sistema</param>
        /// <param name="system">Sistema</param>
        /// <returns>True se sistema for válido</returns>
        public static bool TryGetSystem(string systemName, out SystemComponent system)
        {
            system = GetSystem(systemName, false);

            return !system.IsEmpty;
        }

        private static SystemComponent GetSystem(string systemName, bool throwError)
        {
            if (string.IsNullOrEmpty(systemName))
                throw new ArgumentNullException("systemName");

            RegistryKey key = ApplicationConfiguration.GetApplicationRegistryKey(systemName, throwError);

            string systemDescription = Convert.ToString(key != null ? key.GetValue("SystemDescription") : null);

            if (!string.IsNullOrEmpty(systemDescription))
            {
                return new SystemComponent(systemName, systemDescription, SystemComponentType.System);
            }
            else
            {
                if (throwError)
                    throw new Exception(string.Format("Cannot find SystemDescription in registry key '{0}'", string.Format(@"{0}\SOFTWARE\{1}", Registry.LocalMachine.ToString(), systemName)));
                else
                    return SystemComponent.Empty;
            }
        }
    }
}